package io.renren.modules.photo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.modules.photo.dao.PhotoInfoDao;
import io.renren.modules.photo.dao.PhotoLabelRelaDao;
import io.renren.modules.photo.dto.PhotoInfoDTO;
import io.renren.modules.photo.dto.PhotoInfoSaveDTO;
import io.renren.modules.photo.dto.PhotoInfoUpdateLabelDTO;
import io.renren.modules.photo.entity.PhotoInfoEntity;
import io.renren.modules.photo.entity.PhotoLabelRelaEntity;
import io.renren.modules.photo.service.PhotoInfoService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author huan.xu
 * @Date 2022/11/2 11:21
 * @Description 照片信息
 */
@Service
public class PhotoInfoServiceImpl extends BaseServiceImpl<PhotoInfoDao, PhotoInfoEntity> implements
    PhotoInfoService {

  @Autowired
  private PhotoLabelRelaDao photoLabelRelaDao;

  @Override
  public PageData<PhotoInfoDTO> page(Map<String, Object> params) {
    //转换成like
    paramsToLike(params, "name");
    String searchLabelIds = MapUtils.getString(params, "labelIds");
    if (StringUtils.isNotBlank(searchLabelIds)) {
      params.put("labelIds", searchLabelIds.split(","));
    }

    //分页
    IPage<PhotoInfoEntity> page = getPage(params, Constant.CREATE_DATE, false);

    //查询
    List<PhotoInfoDTO> list = baseDao.getInfoList(params);
    list.stream().forEach(photoInfoDTO -> {
      List<Map> photoLabelList = photoLabelRelaDao
          .findPhotoLabelByPhotoInfoId(photoInfoDTO.getId());
      if (CollectionUtils.isNotEmpty(photoLabelList)) {
        String labelIds = photoLabelList.stream()
            .map(map -> MapUtils.getString(map, "labelId"))
            .collect(Collectors.joining(","));
        String labelNames = photoLabelList.stream()
            .map(map -> MapUtils.getString(map, "labelName"))
            .collect(Collectors.joining(","));
        photoInfoDTO.setPhotoLabelIds(labelIds);
        photoInfoDTO.setPhotoLabelNames(labelNames);
      }
    });

    return getPageData(list, page.getTotal(), PhotoInfoDTO.class);
  }

  /**
   * 保存照片
   */
  @Override
  public void save(PhotoInfoSaveDTO photoInfoSaveDTO) {
    if (photoInfoSaveDTO != null) {
      List<PhotoInfoDTO> photoInfoList = photoInfoSaveDTO.getPhotoInfoList();
      List<Long> photoLabelList = photoInfoSaveDTO.getPhotoLabelList();
      if (CollectionUtils.isNotEmpty(photoInfoList)) {
        for (PhotoInfoDTO photoInfoDTO : photoInfoList) {
          // 保存照片信息
          PhotoInfoEntity photoInfoEntity = new PhotoInfoEntity();
          String fileName = photoInfoDTO.getName();
          photoInfoEntity.setName(fileName);
          photoInfoEntity.setFileType(FilenameUtils.getExtension(fileName));
          photoInfoEntity.setSize(photoInfoDTO.getSize());
          photoInfoEntity.setDownloadUrl(photoInfoDTO.getDownloadUrl());
          photoInfoEntity.setStoragePath(photoInfoDTO.getStoragePath());
          baseDao.insert(photoInfoEntity);

          //保存照片和标签的关系
          if (CollectionUtils.isNotEmpty(photoLabelList)) {
            for (Long labelId : photoLabelList) {
              PhotoLabelRelaEntity photoLabelRelaEntity = new PhotoLabelRelaEntity();
              photoLabelRelaEntity.setInfoId(photoInfoEntity.getId());
              photoLabelRelaEntity.setLabelId(labelId);
              photoLabelRelaDao.insert(photoLabelRelaEntity);
            }
          }
        }
      }
    }
  }

  /**
   * 删除照片
   */
  @Override
  public void delete(Long[] ids) {
    List<Long> photoInfoIds = Arrays.asList(ids);
    // 先删除照片标签关系表
    for (Long photoInfoId : photoInfoIds) {
      photoLabelRelaDao.deletePhotoLabelRelaByPhotoInfoId(photoInfoId);
    }

    //再删除照片
    baseDao.deleteBatchIds(photoInfoIds);
  }

  /**
   * 根据照片id查询对应的标签信息
   */
  @Override
  public List<Map> getPhotoLabelByInfoId(Long infoId) {
    List<Map> photoLabelList = photoLabelRelaDao
        .findPhotoLabelByPhotoInfoId(infoId);
    List<Map> returnListMap = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(photoLabelList)) {
      returnListMap = photoLabelList.stream()
          .map(map -> {
            Map labelMap = new HashMap(16);
            labelMap.put("id", MapUtils.getString(map, "labelId"));
            labelMap.put("name", MapUtils.getString(map, "labelName"));
            return labelMap;
          })
          .collect(Collectors.toList());
    }
    return returnListMap;
  }

  /**
   * 修改照片标签
   */
  @Override
  public void updateLabel(PhotoInfoUpdateLabelDTO updateLabelDTO) {
    // 先删除之前的标签
    photoLabelRelaDao.deletePhotoLabelRelaByPhotoInfoId(updateLabelDTO.getId());

    // 重新保存标签
    List<Long> photoLabelList = updateLabelDTO.getPhotoLabelList();
    if (CollectionUtils.isNotEmpty(photoLabelList)) {
      for (Long labelId : photoLabelList) {
        PhotoLabelRelaEntity photoLabelRelaEntity = new PhotoLabelRelaEntity();
        photoLabelRelaEntity.setInfoId(updateLabelDTO.getId());
        photoLabelRelaEntity.setLabelId(labelId);
        photoLabelRelaDao.insert(photoLabelRelaEntity);
      }
    }

  }
}
